package com.ambimmort.shj.service.dpi.users.group.adsl;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.common.policy_service.PolicySupport;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Dpiusergroup;
import cn.net.greenet.db.bean.UgUsername;
import com.ambimmort.umessage.UcType;
import com.ambimmort.umessage.uc.outgoing.vm.x40;
import com.ambimmort.umessage.util.HexUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * Created by Administrator on 2015/7/30.
 */
@Service("adslDeleteService")
public class DeleteService {

    @Autowired
    private SessionFactory hibernateSessionFactory;
    ResultSet rs =null;
    BaseDao baseDao = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }
    /**
     *
     * @param json
     * @throws Throwable
     */
    public void adslDelete(String json) throws Throwable{
//        String json = request.getParameter("json");
        JSONObject jsonObj = JSONObject.fromObject(json);
        String groupId = jsonObj.getString("groupId");
        String instanceName = jsonObj.getString("instanceName");
        JSONArray userids = jsonObj.getJSONArray("ids");

        java.util.List<x40> msgs = new ArrayList<x40>();
        x40.User user = null;
        x40 msg = null;

        init();
        String sql="select * from dpiusergroup where id="+groupId;
        Object[] params ={};
        rs=baseDao.executeSQL(sql,params);
        try {
            UgUsername ptr = null;
            Object objPtr = null;
            for (int i = 0; i < userids.size(); i++) {
                while (rs.next()) {
                    String sq = "delete from ug_username where DPIUserGroup_id=" + groupId+" and id="+userids;
                    Object[] param = {};
                    baseDao.executeUpdate(sq, param);
                }
                ptr = (UgUsername) objPtr;

                if ((i) % 2000 == 0) {
                    msg = new x40();
                    msg.getHeader().setMessageNo(UcType.newUINT4(0));
                    msg.getHeader().setMessageSequenceNo(UcType.newUINT4(0));
                    msg.getHeader().setMessageType(new UcType.UINT1(new byte[]{(byte) 0x40}));
                    msg.setAction(UcType.newUINT1(2));
                    msg.setUserGroupName(UcType.newUString_UINT1(rs.getString("name")));
                    msg.setUserGroupNo(UcType.newUINT2(rs.getInt("DPIUserGroup_id")));
                    msg.setMessageSerialNo(UcType.newUINT4(0));
                    msgs.add(msg);
                }
                user = new x40.User();
                user.setUserName(UcType.newUString_UINT1(ptr.getName()));
                user.setUserType(UcType.newUINT1(1));
                msg.getUserNum().add(user);
            }
            for (x40 m : msgs) {
                System.out.println(JSONObject.fromObject(m.toJSON()).toString(4));
                System.out.println(HexUtil.getHex(m.toBytes()));
                String comment = "【"+rs.getInt("DPIUserGroup_id")+"("+rs.getString("name")+")"+"】【user】【＋】【"+userids.size()+"】";
                PolicySupport.getInstance().getApi().addPolicy("0x40", rs.getString("instanceName"), m.toJSON(), comment);
            }


//        PrintWriter out = response.getWriter();
//        SessionFactory sf = HibernateUtil.getSessionFactory();
//        Session session = sf.openSession();
//        Transaction tx = session.beginTransaction();

//        java.util.List<x40> msgs = new ArrayList<x40>();
//        x40.User user = null;
//        x40 msg = null;
//        Dpiusergroup groupBean = (Dpiusergroup) session.get(Dpiusergroup.class, Integer.parseInt(groupId));
//        try {
//            UgUsername ptr = null;
//            Object objPtr = null;
//            for (int i = 0; i < userids.size(); i++) {
//                objPtr = session.get(UgUsername.class, userids.getInt(i));
//                if (objPtr == null) {
//                    continue;
//                }
//                ptr = (UgUsername) objPtr;
//
//                session.delete(ptr);

//                if ((i) % 2000 == 0) {
//                    msg = new x40();
//                    msg.getHeader().setMessageNo(UcType.newUINT4(0));
//                    msg.getHeader().setMessageSequenceNo(UcType.newUINT4(0));
//                    msg.getHeader().setMessageType(new UcType.UINT1(new byte[]{(byte) 0x40}));
//                    msg.setAction(UcType.newUINT1(2));
//                    msg.setUserGroupName(UcType.newUString_UINT1(groupBean.getName()));
//                    msg.setUserGroupNo(UcType.newUINT2(groupBean.getId()));
//                    msg.setMessageSerialNo(UcType.newUINT4(0));
//                    msgs.add(msg);
//                }
//                user = new x40.User();
//                user.setUserName(UcType.newUString_UINT1(ptr.getName()));
//                user.setUserType(UcType.newUINT1(1));
//                msg.getUserNum().add(user);
//            }
//            tx.commit();

//            for (x40 m : msgs) {
//                System.out.println(JSONObject.fromObject(m.toJSON()).toString(4));
//                System.out.println(HexUtil.getHex(m.toBytes()));
//                String comment = "【"+groupBean.getId()+"("+groupBean.getName()+")"+"】【user】【＋】【"+userids.size()+"】";
//                PolicySupport.getInstance().getApi().addPolicy("0x40", groupBean.getInstanceName(), m.toJSON(), comment);
//            }

//            out.println("{\"result\":\"true\"}");
        } catch (Exception e) {
            e.printStackTrace();
//            out.println("{\"result\":\"false\"}");
//            tx.rollback();
        } finally {
            close();
//            out.close();
//            session.close();
        }

    }
}
